package com.hbnu.thread;

/**
 * @author kittychen
 * @since 2025-10-23 20:03
 */
public class Box {
    private int number = 0;

    public synchronized void add() {
        if (number >= 20) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        } else {
            number++;
            System.out.println(Thread.currentThread().getName() + "生产了第" + number + "号牛奶");
            notifyAll();
        }
    }

    public synchronized void get() {
        if (number <= 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        } else {
            System.out.println(Thread.currentThread().getName() + "消费了第" + number + "号牛奶");
            number--;
            notifyAll();
        }
    }
}
